/**
 * @file     groupAnagrams.js
 * @brief    [49. 字母异位词分组](https://leetcode.cn/problems/group-anagrams/description/)
 * @author   Zhu
 * @date     2023-06-17 23:46
 */

/**
 * @param {string[]} strs
 * @return {string[][]}
 */
var groupAnagrams = function (strs) {
    const res = new Map();
    for (const str of strs) {
        const fp = fingerprint(str);
        if (res.has(fp)) {
            const curGroup = res.get(fp);
            curGroup.push(str);
        } else {
            res.set(fp, [str]);
        }
    }

    return [...res.values()];

    function fingerprint(str) {
        return str.split('').sort().join('');
    }
};
